﻿using System;
using System.Collections.Generic;
using System.Text;
using UmlDesigner.Gui;

namespace UmlDesigner.Model.WorkUnits
{
    internal class NewLinkWorkItem : IWorkUnit
    {
        public NewLinkWorkItem(LinkWidget newLink)
        {
            link = newLink;
        }

        private LinkWidget link = null;

        #region IWorkUnit Members

        public void Do(UmlControl umlControl)
        {
            // Allocate off slots for the link.
            if (link.GetSource() == link.GetDestination())
            {
                link.AllocateSlotsForCircularRef();
            }
            else
            {
                link.AllocateSlots();
            }

            link.RecalculateAllSlots();

            umlControl.currentDocument.items.Add(link);
            RefreshControl(umlControl);
        }

        public void Undo(UmlControl umlControl)
        {
            umlControl.currentDocument.items.Remove(link);
            RefreshControl(umlControl);    
        }

        private static void RefreshControl(UmlControl umlControl)
        {
            umlControl.currentDocument.isModified = true;
            umlControl.Invalidate();
        }



        #endregion
    }
}
